//
//  BindZaoShangViewController.m
//  ZFPlatform
//
//  Created by magus on 11-6-4.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BindZaoShang2ViewController.h"
#import "CTextField.h"
#import "BindZaoShang3ViewController.h"


@implementation BindZaoShang2ViewController

@synthesize panel;
@synthesize cardNum;
@synthesize cardError;
@synthesize	phoneNum;
@synthesize phoneError;
@synthesize	nextBtn;


//通过xib资源初始化视图控制器
//
+ (BindZaoShang2ViewController*) InitFromXib
{
	self = [[BindZaoShang2ViewController alloc] 
			initWithNibName:@"BindZaoShang2ViewController" bundle:nil];
	return self;
}


//初始化界面
//
- (void) InitAllViews
{
	//设置导航栏标题
	[self setTitle:@"招行信用卡绑定"];
	
	//设置背景面板成圆角显示
	[panel.layer setCornerRadius:12.0];
	
	//设置“绑定招行卡”按钮图片
	UIImage* buttonImg = [UIImage imageNamed:@"按钮（大）.png"];
	[nextBtn setBackgroundImage:buttonImg forState:UIControlStateNormal];
	[buttonImg release];
	
	//设置导航栏左按钮
	UIImage* image = [UIImage imageNamed: @"导航栏按钮（返回）.png"];
	CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
	UIButton* leftButton = [[UIButton alloc] initWithFrame:rect];
	[leftButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
	[leftButton setTitle:@" 返回" forState:UIControlStateNormal];
	[leftButton setBackgroundImage:image forState:UIControlStateNormal];
	[leftButton addTarget:self action:@selector(GoBack:) forControlEvents:UIControlEventTouchDown];
	UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
	self.navigationItem.leftBarButtonItem = leftItem;
	[leftItem release];
	[leftButton release];
	[image release];
}


//响应“返回”导航按钮
//
- (void) GoBack: (id) sender
{
	[self.navigationController popViewControllerAnimated:YES];
}


//使按return键后退出软键盘
//
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


//检查信用卡号是否合法
//
- (BOOL) CheckCard: (NSString*) strPhone
{
	int len = [strPhone length];
	
	if (len == 0)
	{
		cardError.text = @"不能为空";
		return NO;
	}
	
    NSString *regex = @"[0-9]{6}"; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
	if ( ! [predicate evaluateWithObject:strPhone] )
	{
		cardError.text = @"格式错误";
		return NO;
	}
	
    return YES;
}


//检查手机号码是否合法
//
- (BOOL) CheckPhone: (NSString*) strPhone
{
	int len = [strPhone length];
	
	if (len == 0)
	{
		phoneError.text = @"不能为空";
		return NO;
	}
	
    NSString *regex = @"[0-9]{11}"; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
	if ( ! [predicate evaluateWithObject:strPhone] )
	{
		phoneError.text = @"格式错误";
		return NO;
	}
	
    return YES;
}


//响应文本框开始编辑
//
- (IBAction) OnEditBegin: (id) sender
{
	CTextField* textField = (CTextField*)sender;
	
	[textField SetBorderWidth:4.0];
	[textField SetCornerRadius:8.0];
	[textField SetBorderColor:[UIColor orangeColor]];
	[textField setTextColor:[UIColor blackColor]];
	
	if (textField == cardNum)
	{
		cardError.text = @"";
	}
	else if(textField == phoneNum)
	{
		phoneError.text = @"";
	}
}


//响应文本框编辑完毕
//
- (IBAction) OnEditEnd: (id) sender
{
	CTextField* textField = (CTextField*)sender;
	
	[textField SetBorderWidth:0.0];
	[textField setTextColor:[UIColor lightGrayColor]];
	
	if (textField == cardNum)
	{
		NSString* strCard = [cardNum text];
		[self CheckCard:strCard];
	}
	else if(textField == phoneNum)
	{
		NSString* strPhone = [phoneNum text];
		[self CheckPhone:strPhone];
	}
}


//响应"下一步"按钮
//
- (IBAction) OnClick_Next: (id) sender
{
	NSString* strCard = [cardNum text];
	NSString* strPhone = [phoneNum text];
	
	if(![self CheckCard:strCard] || ![self CheckPhone:strPhone])
	{
		return;
	}
	
	if(bindZaoShang3ViewController == nil)
		bindZaoShang3ViewController = [BindZaoShang3ViewController InitFromXib];
	
	[self.navigationController pushViewController:bindZaoShang3ViewController animated:YES];
}


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	[self InitAllViews];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc
{
	[bindZaoShang3ViewController release];
	[nextBtn release];
	[phoneError release];
	[phoneNum release];
	[cardError release];
	[cardNum release];
	[panel release];
	
    [super dealloc];
}


@end
